﻿// See https://aka.ms/new-console-template for more information
using System;
using System.Collections.Generic;
using System.Linq;

class Order
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Customer { get; set; }
    public double Number { get; set; }

    public Order(int id, string name, string customer, double number)
    {
        Id = id;
        Name = name;
        Customer = customer;
        Number = number;
    }

    public override bool Equals(object obj)
    {
        if (obj is Order other)
        {
            return Id == other.Id && Name == other.Name && Customer == other.Customer && Number == other.Number;
        }
        return false;
    }

    public override int GetHashCode()
    {
        return Id;
    }

    public override string ToString()
    {
        return $"Id: {Id}, Name: {Name}, Customer: {Customer}, Number: {Number}";
    }
}

class OrderDetails : Order
{
    public OrderDetails(int id, string name, string customer, double number)
        : base(id, name, customer, number)
    {
    }

    // No need to override Equals and GetHashCode as Order's implementation is sufficient for OrderDetails.
}

class OrderService
{
    private List<Order> orders = new List<Order>();

    public void AddOrder(Order order)
    {
        if (!orders.Contains(order))
        {
            orders.Add(order);
        }
        else
        {
            throw new InvalidOperationException("订单已存在！");
        }
    }

    public void RemoveOrder(int orderId)
    {
        var orderToRemove = orders.FirstOrDefault(o => o.Id == orderId);
        if (orderToRemove != null)
        {
            orders.Remove(orderToRemove);
        }
        else
        {
            throw new InvalidOperationException("找不到指定的订单！");
        }
    }

    public void ModifyOrder(int orderId, string newName, double newNumber)
    {
        var orderToModify = orders.FirstOrDefault(o => o.Id == orderId);
        if (orderToModify != null)
        {
            orderToModify.Name = newName;
            orderToModify.Number = newNumber;
        }
        else
        {
            throw new InvalidOperationException("找不到指定的订单！");
        }
    }

    public List<Order> QueryOrdersByAmount()
    {
        return orders.OrderBy(o => o.Number).ToList();
    }

    public void SortOrdersBy(Func<Order, object> keySelector)
    {
        orders = orders.OrderBy(keySelector).ToList();
    }
}

class Program
{
    static void Main(string[] args)
    {
        var orderService = new OrderService();
        var order1 = new Order(1, "Product A", "Alice", 100.0);
        var order2 = new Order(2, "Product B", "Bob", 200.0);
        var order3 = new Order(3, "Product C", "Casey", 300.0);
        var order4 = new Order(4, "Product D", "David", 100.0);

        orderService.AddOrder(order1);
        orderService.AddOrder(order2);
        orderService.AddOrder(order3);
        orderService.AddOrder(order4);
        var sortedOrders1 = orderService.QueryOrdersByAmount();
        Console.WriteLine("Sorted Orders by Amount:");
        foreach (var order in sortedOrders1)
        {
            Console.WriteLine(order.ToString());
        }

        try
        {
            orderService.RemoveOrder(1);//移除Alice
        }
        catch (InvalidOperationException ex)
        {
            Console.WriteLine(ex.Message);
        }

        orderService.ModifyOrder(2, "Product C", 200.0);

        var sortedOrders2 = orderService.QueryOrdersByAmount();
        Console.WriteLine("Sorted Orders after Modification:");
        foreach (var order in sortedOrders2)
        {
            Console.WriteLine(order.ToString());
        }

        // Custom sorting example
        orderService.SortOrdersBy(o => o.Customer);
        var sortedOrders3 = orderService.QueryOrdersByAmount();
        Console.WriteLine("Sorted Orders by Customer:");
        foreach (var order in sortedOrders3)
        {
            Console.WriteLine(order.ToString());
        }
    }
}
